package com.devmindset.legoframework.usage.client.monitors;

import java.util.List;

import com.devmindset.legoframework.client.mvc.EventMonitor;
import com.devmindset.legoframework.client.mvc.LegoEvent;
import com.devmindset.legoframework.client.mvc.LegoModel;
import com.devmindset.legoframework.client.mvc.metadata.EventMonitorStory;
import com.devmindset.legoframework.client.mvc.metadata.Stories;
import com.devmindset.legoframework.client.mvc.util.ErrorDialog;
import com.devmindset.legoframework.usage.client.component.OrganizerComponent;
import com.devmindset.legoframework.usage.client.component.OrganizerHeaderComponent;
import com.devmindset.legoframework.usage.client.event.Events;
import com.devmindset.legoframework.usage.client.model.CalEvent;
import com.devmindset.legoframework.usage.client.service.UsageService;
import com.devmindset.legoframework.usage.client.util.Util;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class OrganizerScreenMonitor extends EventMonitor {
	
	private OrganizerHeaderComponent headerComponent = new OrganizerHeaderComponent();
	private OrganizerComponent organizerComponent = new OrganizerComponent();
	
	/** {@inheritDoc} */
	@Override
	public void handleEvent(LegoEvent event) {
		if (event.matches(Events.RENDER_ORGANIZER_HEADER_COMPONENT)) {
			headerComponent.render(new LegoModel(null));
		} else if (event.matches(Events.RENDER_ORGANIZER_COMPONENT)) {
			UsageService.get().getCalendarEvents(new AsyncCallback<List<CalEvent>>() {
				public void onSuccess(List<CalEvent> arg0) {
					organizerComponent.render(new LegoModel(Util.toCalendarEvents(arg0)));
				}
				public void onFailure(Throwable arg0) {
					ErrorDialog.logAndShow(arg0);
				}
			});
		}
	}
	
	/** {@inheritDoc} */
	public EventMonitorStory myStory() {
		return Stories.eventMonitor()
			.canHandle(
					Events.RENDER_ORGANIZER_HEADER_COMPONENT,
					Events.RENDER_ORGANIZER_COMPONENT
			)
			.knownAs("Organizer Screen Monitor");
	}

}